// 二分搜索的前提是数组必须有序

Array.prototype.binarySearch = function(item) {
  let low = 0
  let high = this.length-1
  while(this[low] < this[high]) {
    let mid = Math.floor((low+high)/2)
    let element = this[mid]
    if(element < item) {
      low = mid + 1
    } else if (element > item ) {
      high = mid - 1
    } else {
      return mid
    }
  }
  return -1
}

const res = [1,2,3,4,5].binarySearch(3)